#include "../comm.h"
class Solution {
public:
    int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) {
        vector<int> p;
        for(int i = 0; i < profit.size(); ++i) p.push_back(i);
        sort(p.begin(), p.end(), [&](int a, int b){
            return difficulty[a] < difficulty[b];
        });
        vector<int> idx{0}, pro{0};
        for(int i = 0; i < profit.size(); ++i)
        {
            int j = difficulty[p[i]];
            // cout << profit[p[i]] << ' ';
            if(idx.back() < j)
                idx.push_back(j), pro.push_back(max(pro.back(), profit[p[i]]));
            else
                pro.back() = max(pro.back(), profit[p[i]]);
        }
        // cout << endl;
        // for(int i = 0; i < pro.size(); ++i)
        //     cout << idx[i] << ' ';
        // cout << endl;
        int ans = 0;
        for(auto e: worker)
        {
            int l = 0, r = idx.size() - 1;
            while(l < r)
            {
                int mid = (l + r + 1) >> 1;
                if(idx[mid] <= e) l = mid;
                else r = mid - 1;
            }
            ans += pro[l];
        }
        return ans;
    }
};